package com.lucky.yingying

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController


/**
 * http://127.0.0.1:8888/api/ops/init?secret=huyingying88888888&city=chengdu
 */
@RestController
@RequestMapping("/api/ops")
class OpsService {
    val SECRET = "huyingying88888888"

    @GetMapping("/init")
    fun luck(
        @RequestParam("secret") secret: String,
        @RequestParam("city") city: String,
    ): APIResult<Boolean> {
        if (SECRET != secret) return APIResult("secret有误!", false, false)

        NameList.nameList.filter { it.city == city }.forEach {
            it.lucky = false
            it.chou = false
        }

        return APIResult("${city} 抽奖池重置成功!", true, true)
    }

    @GetMapping("/resetTotalCount")
    fun resetTotalCount(
        @RequestParam("secret") secret: String,
        @RequestParam("city") city: String,
        @RequestParam("totalCount") totalCount: Int,
    ): APIResult<Boolean> {
        if (SECRET != secret) return APIResult("secret有误!", false, false)

        when (city) {
            "成都" -> CHENGDU_TOTAL_LUCK = totalCount
            "重庆" -> CHONGQING_TOTAL_LUCK = totalCount
            "南京" -> NANJING_TOTAL_LUCK = totalCount
            "南昌" -> NANCHANG_TOTAL_LUCK = totalCount
            "武汉" -> WUHAN_TOTAL_LUCK = totalCount
        }

        return APIResult("${city} 门票数量设置成功!", true, true)
    }

    @GetMapping("/getTotalCount")
    fun getTotalCount(
        @RequestParam("secret") secret: String,
        @RequestParam("city") city: String,
    ): APIResult<Int> {
        if (SECRET != secret) return APIResult("secret有误!", false, false)

        var totalCount = 0

        when (city) {
            "成都" -> totalCount = CHENGDU_TOTAL_LUCK
            "重庆" -> totalCount = CHONGQING_TOTAL_LUCK
            "南京" -> totalCount = NANJING_TOTAL_LUCK
            "南昌" -> totalCount = NANCHANG_TOTAL_LUCK
            "武汉" -> totalCount = WUHAN_TOTAL_LUCK
        }

        return APIResult("${city} 门票数量", true, totalCount)
    }

}

